package com.ellecity06.common.http.exception

import com.ellecity06.common.utils.debugLog
import com.google.gson.JsonParseException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException

/**
 * @author zavier
 * @time 2023/4/14 12:22
 * @des TODO
 */
class ExceptionHandle {
    companion object {
        fun handleException(e: Throwable): HttpErrorException {
            debugLog(e)
            //均视为网络错误
            return when (e) {
                is SocketTimeoutException,
                is ConnectException -> HttpErrorException(message = "网络连接异常")
                is HttpException -> {
                    if (e.code() >= 500) {
                        HttpErrorException(message = "服务器异常", code = e.code())
                    } else {
                        HttpErrorException(message = "找不到接口", code = e.code())
                    }
                }
                is JsonParseException,
                is JSONException,
                is ParseException -> HttpErrorException(message = "数据解析异常")
                is UnknownHostException -> HttpErrorException(message = "服务器正着维护，请稍候重试")
                is IllegalArgumentException -> HttpErrorException(message = "非法参数错误")
                else -> HttpErrorException(message = e.message ?: "未知错误")

            }

        }

    }
}